import { _decorator, Component, EditBox, Node } from 'cc';
import { ListRender } from './ListRender';
import { TipsManager } from '../Model/Tips/TipsManager';
import { DropDownListData } from '../Model/Tips/TipsDataBase';
import { EventManager } from '../Manager/EventManager';
import { BaseEventType } from '../Config/BaseEnum';
const { ccclass, property, menu } = _decorator;

@ccclass('DropdownBox')
@menu('myComponent/DropdownBox')
export class DropdownBox extends Component {
  @property(EditBox)
  edit: EditBox;

  private _data: unknown[] = [];
  judgeFun: (input: string, data: unknown) => boolean;
  input: (data: unknown) => string;

  set data(data: unknown[]) {
    this._data = data;
    if (this._data.length <= 0) return;
    if (this.input != null) {
      this.edit.string = this.input(this._data[0]);
    } else {
      this.edit.string = this._data[0].toString();
    }
  }

  get data(): unknown[] {
    return this._data;
  }

  onFocue() {
    const tips = DropDownListData.pop(this.node, [], this._data, this.judgeFun, this.onChose);
    TipsManager.Singleton.showTips(tips);
  }

  onChange(text) {
    EventManager.Ins().emit(BaseEventType.GAME_DROPDOWN_CHANGE, text);
  }

  onChose(...args) {
    const item: Node = args[0];
    const sp = item.getComponent(ListRender);
    if (this.input != null) {
      this.edit.string = this.input(sp.data);
    } else {
      this.edit.string = sp.data.toString();
    }
    TipsManager.Singleton.canelTip();
  }
}
